package com.walker.elk.netty.socket;

import com.walker.elk.netty.socket.server.WalkerServerChannelInitializer;

import io.netty.bootstrap.ServerBootstrap;
import io.netty.channel.ChannelFuture;
import io.netty.channel.ChannelOption;
import io.netty.channel.EventLoopGroup;
import io.netty.channel.nio.NioEventLoopGroup;
import io.netty.channel.socket.nio.NioServerSocketChannel;

public class WalkerNettySocketServer {

	public static void main(String[] args) {
		EventLoopGroup boos = new NioEventLoopGroup();  //接受请求
		EventLoopGroup worker = new NioEventLoopGroup();//处理请求
		

		try {
			ServerBootstrap serverBootstrap = new ServerBootstrap();
			serverBootstrap.group(boos, worker).channel(NioServerSocketChannel.class). //这里告诉Channel如何接收新的连接
			childHandler(new WalkerServerChannelInitializer());                              //指定初始化channel对象
			serverBootstrap.option(ChannelOption.SO_BACKLOG, 1024);          //设置TCP缓存区
			serverBootstrap.option(ChannelOption.SO_SNDBUF, 1024 * 32);      //发送缓冲大小
			serverBootstrap.option(ChannelOption.SO_RCVBUF, 1024 * 32);      //接受缓冲大小
			serverBootstrap.option(ChannelOption.SO_KEEPALIVE, true);        //保持连接
			ChannelFuture channelFuture = serverBootstrap.bind(8899).sync(); //绑定端口
			channelFuture.channel().closeFuture().sync();
		} catch (Exception e) {
			worker.shutdownGracefully();
			boos.shutdownGracefully();
			e.printStackTrace();
		}
	}

}
